package io.github.xbeeant.eoffice.rest;

import io.github.xbeeant.antdesign.TableResponse;
import io.github.xbeeant.core.ApiResponse;
import io.github.xbeeant.core.ErrorCodeConstant;
import io.github.xbeeant.core.JavaBeansHelper;
import io.github.xbeeant.eoffice.antdesign.TreeNode;
import io.github.xbeeant.eoffice.model.Folder;
import io.github.xbeeant.eoffice.model.FolderUser;
import io.github.xbeeant.eoffice.model.User;
import io.github.xbeeant.eoffice.rest.vo.FolderAuthVo;
import io.github.xbeeant.eoffice.rest.vo.FolderUserVo;
import io.github.xbeeant.eoffice.service.IFolderService;
import io.github.xbeeant.eoffice.service.IFolderUserService;
import io.github.xbeeant.eoffice.service.IUserService;
import io.github.xbeeant.eoffice.util.SecurityHelper;
import io.github.xbeeant.spring.mybatis.antdesign.PageRequest;
import io.github.xbeeant.spring.mybatis.antdesign.PageResponse;
import io.github.xbeeant.spring.mybatis.pagehelper.PageBounds;
import io.github.xbeeant.spring.security.LoginUser;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaobiao
 * @version 2021/7/6
 */
@RestController
@RequestMapping("api/folder")
public class FolderRestController {

    @Autowired
    private IFolderService folderService;

    @Autowired
    private IFolderUserService folderUserService;

    @GetMapping("auth")
    public ApiResponse<TableResponse<FolderUserVo>> auth(Long fid, PageRequest pageRequest) {
        ApiResponse<TableResponse<FolderUserVo>> result = new ApiResponse<>();
        PageBounds pageBounds = pageRequest.getPageBounds();
        if (ObjectUtils.isEmpty(pageBounds.getOrders())) {
            pageBounds.setOrders("create_at desc");
        }
        FolderUser record = new FolderUser();
        record.setFid(fid);
        ApiResponse<PageResponse<FolderUserVo>> pageResponseApiResponse = folderUserService.fuzzySearchVoByPager(record, pageBounds);
        PageResponse<FolderUserVo> list = pageResponseApiResponse.getData();

        if (CollectionUtils.isEmpty(list)) {
            result.setResult(ErrorCodeConstant.NO_MATCH, ErrorCodeConstant.NO_MATCH_MSG);
            return result;
        }

        TableResponse<FolderUserVo> pageResponse = new TableResponse<>();
        pageResponse.setList(list);
        pageResponse.setPagination(list.getPagination());
        result.setData(pageResponse);
        return result;
    }

    @DeleteMapping("auth")
    public ApiResponse<Integer> authRemove(Long id) {
        // todo 平行权限问题
        return folderUserService.deleteByPrimaryKey(id);
    }

    @PostMapping("auth")
    public ApiResponse<FolderUser> authUser(FolderAuthVo record) {

        ApiResponse<FolderUser> result = new ApiResponse<>();
        // fid 和当前用户的 权限校验, 仿真非当前fid的创建者通过接口往别人的目录里写数据
        ApiResponse<Folder> folderApiResponse = folderService.selectByPrimaryKey(record.getFid());
        LoginUser<User> userLoginUser = SecurityHelper.currentUser();
        if (!folderApiResponse.getData().getCreateBy().equalsIgnoreCase(userLoginUser.getUserId())) {
            result.setResult(1, "您无法对他人的目录进行管理");
            return result;
        }

        FolderUser folderUser = new FolderUser();
        folderUser.setUid(record.getUid());
        folderUser.setFid(record.getFid());
        for (String perm : record.getPerms()) {
            JavaBeansHelper.setter(folderUser, perm, true);
        }

        // 清除可能被传值的异常数据
        folderUser.setCreateBy(null);
        folderUser.setCreateAt(null);
        folderUser.setUpdateBy(null);
        folderUser.setUpdateAt(null);

        return folderUserService.insertSelective(folderUser);
    }

    @GetMapping
    public List<TreeNode> fetch() {
        LoginUser<User> userLoginUser = SecurityHelper.currentUser();
        return folderService.childNodes(userLoginUser.getUserId());
    }

    @PostMapping
    public ApiResponse<Folder> post(Folder folder) {
        LoginUser<User> userLoginUser = SecurityHelper.currentUser();
        folder.setUsername(userLoginUser.getUserNickname());
        return folderService.insertSelective(folder);
    }
}
